package com.example.demo.redis;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @Author CHAS
 * 2024/9/8 16:22
 **/
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTestIncr {
    @Autowired
    private RedisTemplate<String, Integer> redisTemplate;
    public void setInterCounter(final String key, Integer value) {
        try {
            redisTemplate.opsForValue().set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void redisCounterInit(){
        Integer a = new Integer(0);
        setInterCounter("counter",a);
        System.out.println("add counter");
        System.out.println(redisTemplate.opsForValue().get("counter"));
    }
    @Test
    public void redisCounterIncr() {
        redisTemplate.opsForValue().increment("counter",12);
        System.out.println(redisTemplate.opsForValue().get("counter"));
    }
}
